# @Author   : 懒羊羊
# @File     : basepage.py
# @Time     : 2024/12/12 11:03

from appium import webdriver
from appium.options.common import AppiumOptions
from appium.webdriver import WebElement
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from common.doLog import DoLog

dolog = DoLog().set_get_logger()


class BasePage:
    url = "http://127.0.0.1:4723/wd/hub"

    options = AppiumOptions()

    options.set_capability('platformName', 'Android')
    options.set_capability('platformVersion', '9')
    options.set_capability('deviceName', 'xxx')
    options.set_capability('appPackage', 'com.kkpodcast')
    options.set_capability('appActivity', 'com.kkpodcast.activity.WelcomeActivity')
    options.set_capability('noReset', True)

    def __init__(self, d=None):
        if d is None:
            self.d: WebDriver = webdriver.Remote(self.url, options=self.options)
        else:
            self.d: WebDriver = d
        self.d.implicitly_wait(10)

    def getElement(self, locater):
        try:
            ele: WebElement = WebDriverWait(self.d, 10, 1).until(lambda d: d.find_element(*locater))
            self.d.implicitly_wait(10)
        except Exception as e:
            dolog.error(f"发生异常了，查找元素失败{locater}, 错误原因{e}")
        else:
            dolog.info(f"查看元素{locater}成功")
            return ele
        finally:
            dolog.info("查看元素结束")

    def get_Ele_Value(self, locater):
        print(self.getElement(locater).text)

        return self.getElement(locater).text

    def set_Ele_Value(self, locater, Value):
        e = self.getElement(locater)
        e.clear()
        e.send_keys(Value)
